﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp;

namespace BestClient
{
    public class ClientMessageInteract // WebSocket
    {
        private static string TAG = "ClientMessageInteract";
        private ClientProcessorCallBack clientProcessorCallBack;
        public DataBuffer dataBuffer;
        private WebSocket ws;

        public WebSocket getConnection()
        {
            return ws;
        }

        public bool connectBlocking()
        {
            return ws.ReadyState == WebSocketState.Open;
        }

        public ClientMessageInteract(Uri serverURI, Context context) {
            //super(serverURI);
            ws = new WebSocket(serverURI.ToString(), null);
            ws.OnOpen += onOpen;
            ws.OnMessage += onMessage;
            ws.OnClose += onClose;
            ws.OnError += onError;
           
            dataBuffer = new DataBuffer(this,context);
            

            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(dataBuffer.Run));
            thread.Start();
            
            


            //Executors.defaultThreadFactory().newThread(dataBuffer).start();
        }

        public void setClientProcessorCallBack(ClientProcessorCallBack clientProcessorCallBack) {
            this.clientProcessorCallBack = clientProcessorCallBack;
        }

        public void Run(){
            ws.ConnectAsync();
        }


        public void onOpen(object sender, EventArgs e) {
            //Log.d(TAG, "WebSocket connection is open");
            clientProcessorCallBack.afterOpen(this);
        }

        public void onMessage(object sender, MessageEventArgs e) {
            //Log.d(TAG, "WebSocket receive message ");
            clientProcessorCallBack.afterReceiveMsg(e);
            
        }

        public void onClose(object sender, CloseEventArgs e) {
            //Log.d(TAG, "WebSocket is close");
            clientProcessorCallBack.afterClose(e);
        }

        public void onError(object sender, ErrorEventArgs e) {
            Console.WriteLine(TAG, "error:" + e.Message + ":TRACKED:" + e.Exception.StackTrace);
        }

        //@Override
        public void Send(string text) {
            Console.WriteLine("DZ_" + text);
            //super.send(text);
            ws.Send(text);
            //Log.i(TAG, "send" + text);
            BestData bestData = MessageConverter.fromJson<BestData>(text);

            if (!string.Equals(bestData.sys,BestConstant.SYSTEM_ID_RESPONSE)) {
                dataBuffer.addSendData2Buff(bestData.mid, text);
            }
        }

        public void send(string text)
        {
            Send(text);
        }

        //Daniel.Zhang reserved.
        //@Override
        //public void onWebsocketPong(WebSocket conn, Framedata f) {
        //    super.onWebsocketPong(conn, f);
        //    Log.i(TAG, "onWebsocketPong:" + f.getOpcode());
        //}
    }
}
